package phoenix.mob_android.page_objects;

import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.PageBaseMobAndroidExt;
import phoenix.core.config.extensions.ConfigExt;
import phoenix.core.data_models.UserProfileDataModel.PaymentDetails;
import phoenix.mob_android.locators.base.PaymentPageLocatorsBase;

public class PaymentPage extends PageBaseMobAndroidExt
{
	/**
	 * Initializes the PaymentPage
	 * @param p_manager
	 *            PhoenixManager instance
	 */
	public PaymentPage(PhoenixManager p_manager)
	{
		super(p_manager);
	}

	/**
	 * Gets web elements by site
	 * @return IndexPageLocatorsBase instance
	 */
	public PaymentPageLocatorsBase elements()
	{
		switch (manager.testModel().getSiteName())
		{
			case ConfigExt.SITE_ID_FLIRT:
			{
				return new phoenix.mob_android.locators.flirt.PaymentPageLocators();
			}
			case ConfigExt.SITE_ID_WILDBUDDIES:
			{
				return new phoenix.mob_android.locators.wildbuddies.PaymentPageLocators();
			}
			case ConfigExt.SITE_ID_CHEECKYLOVERS:
			{
				return new phoenix.mob_android.locators.cheeckylovers.PaymentPageLocators();
			}
			default:
			{
				return null;
			}
		}
	}

	public void isOnPaymentPage()
	{
		md().wait(10);
		md().waitForPresence(elements().blockPaymentPage, "Payment page doesn't appear");
		md().waitForPresence(elements().blockMembership6mo, "Block membership 6 month doesn't appear");
	}

	public boolean checkForLoadingIcon()
	{
		return md().isElementDisplayed(elements().blockPageLoading);
	}

	public PaymentPage selectMembership(Integer p_package)
	{
		log().info("Selecting Membership Package " + p_package);
		md().waitForVisibility(elements().blockMembership6mo, "Block select membership doesn't appear");
		switch (p_package)
		{
			case 1:
			{
				md().click(elements().blockMembership3day);
				break;
			}
			case 2:
			{
				md().click(elements().blockMembership1mo);
				break;
			}
			case 3:
			{
				md().click(elements().blockMembership3mo);
				break;
			}
			case 4:
			{
				md().click(elements().blockMembership6mo);
				break;
			}
			default:
			{
				log().error("Error : Incorrect Package parameter = " + p_package);
			}
		}
		md().wait(5);

		return new PaymentPage(manager);
	}

	public void fillPaymentDetails(PaymentDetails p_paymentDataParams, boolean p_expected)
	{
		md().waitForVisibility(elements().inputFieldCardNumber, "Input field card number doesn't appear");
		md().wait(5);
		md().typeAndClear(elements().inputFieldCardNumber, p_paymentDataParams.cardNumber);
		md().hideKeyboardApp();
		md().click(elements().inputFieldCardNumber);
		md().typeAndClear(elements().inputFieldCardName, p_paymentDataParams.cardName);
		md().hideKeyboardApp();
		md().click(elements().inputFieldCardName);
		md().click(elements().selectExpiryDate);
		md().waitForPresence(elements().selectElements, "List of expiry date of the card doesn't appear");
		switch (Boolean.toString(p_expected))
		{
			case "false":
			{
				this.setExpiryDateOfCard(7);
				break;
			}

			case "true":

			{
				this.setExpiryDateOfCard(9);
				break;
			}

			default:
			{
				log().errorAndScreen("Incorrect expected results");
			}
		}
		md().waitForPresence(elements().selectExpiryYear, "Button expiry year of the card doesn't appear");
		md().click(elements().selectExpiryYear);
		md().waitForPresence(elements().selectElements, "List of expiry year of the card doesn't appear");
		this.setExpiryDateOfCard(1);
		md().waitForPresence(elements().inputFieldSecurityNumber, "Input field security number doesn't appear");
		md().typeAndClear(elements().inputFieldSecurityNumber, p_paymentDataParams.securityNumber);
		md().hideKeyboardApp();
		if (manager.isFlirt())
		{
			md().waitForPresence(elements().inputFieldBillingAddress, "Input field billing address doesn't appear");
			md().type(elements().inputFieldBillingAddress, p_paymentDataParams.billingAddress);
			md().hideKeyboardApp();
			md().waitForPresence(elements().inputFieldCity, "Input field city doesn't appear");
			md().type(elements().inputFieldCity, p_paymentDataParams.city);
			md().hideKeyboardApp();
			md().waitForPresence(elements().inputFieldPostcode, "Input field postcode doesn't appear");
			md().type(elements().inputFieldPostcode, p_paymentDataParams.postcode);
			md().hideKeyboardApp();
			md().waitForVisibility(elements().btnSubmit, "Submit button doesn't appear");
		}
		md().wait(2);
		md().click(elements().btnSubmit);
	}

	private void setExpiryDateOfCard(int p_value)
	{
		manager.wd().driver().findElements(elements().selectElements).get(p_value).click();
	}

	public void clickBackAndroidButton()
	{
		manager.md().driver().navigate().back();
	}

}
